package com.wowoai.user.repository.impl;

import com.github.wenhao.jpa.PredicateBuilder;
import com.github.wenhao.jpa.Sorts;
import com.github.wenhao.jpa.Specifications;
import com.wowoai.common.page.Pagination;
import com.wowoai.common.reponsitory.DataJpaBaseDao;
import com.wowoai.user.entity.ScoreOptionLogEntity;
import com.wowoai.user.repository.ScoreOptionLogRepositoryI;
import com.wowoai.user.repository.jpa.ScoreOptionLogJpaRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ScoreOptionLogRepositoryImpl extends DataJpaBaseDao<ScoreOptionLogEntity,Long> implements ScoreOptionLogRepositoryI {

    private ScoreOptionLogJpaRepository scoreOptionLogRepository;

    public ScoreOptionLogRepositoryImpl(ScoreOptionLogJpaRepository dataJpaRepository) {
        super(dataJpaRepository);
        scoreOptionLogRepository = dataJpaRepository;
    }

    @Override
    public Pagination<ScoreOptionLogEntity> getOptionLogPage(Integer pageNum, Integer pageSize, String phoneNumber) {
        PredicateBuilder<ScoreOptionLogEntity> builder = Specifications.and();
        builder.eq("deleted", false);
        builder.eq("phoneNumber", phoneNumber);
        Sort sort = Sorts.builder().desc("createTime").build();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize,sort);
        Page<ScoreOptionLogEntity> wallpaperEntityPage = scoreOptionLogRepository.findAll(builder.build(), pageable);
        List<ScoreOptionLogEntity> wallpaperEntityList = wallpaperEntityPage.getContent();
        return new Pagination<>(wallpaperEntityPage.getPageable().getPageNumber() + 1, wallpaperEntityPage.getPageable().getPageSize(),
                (int) wallpaperEntityPage.getTotalElements(), wallpaperEntityList);
    }
}
